<?php

/**
 * Timeline processing function
 * Using raid object, generates drops
 * @return 
 * @param object $node
 */
function _mewdkp_process_timeline($node) {
  
  if (is_array($node->kills)) {
    
    $query = "SELECT node.nid AS nid
              FROM {node} node 
              LEFT JOIN {content_field_mewdkp_raid} node_data_field_mewdkp_raid ON node.vid = node_data_field_mewdkp_raid.vid
              WHERE (node.type in ('mewdkp_drop')) AND (node_data_field_mewdkp_raid.field_mewdkp_raid_nid = %d)";
    $res = db_query($query,$node->nid);
    while ($rec = db_fetch_object($res)) {
      _mewdkp_silent_delete($rec->nid);
      #drupal_set_message('deleted drop');
    }
    
    foreach ($node->kills as $kill) {
      
      $zone_name = node_load($node->field_mewdkp_zone[0]['nid']);
      $mob_node = _mewdkp_mob_load($kill['name'],$zone_name->title);
      
      $attendance = array();
      
      foreach ($kill['attendance'] as $attended) {
        $attendance[] = array('nid' => $attended);
      }
      
      if (variable_get('mewdkp_use_bank', TRUE)) {
        $bank_node = _mewdkp_bank_load();
        $attendance[] = array('nid' => $bank_node->nid);
      }
      
      foreach ($kill['drops'] as $drop) {
        
        $item_node = node_load($drop['nid']);
        
        if(!$item_node->field_mewdkp_exclude[0]['value']){
          
          if ($drop['note'] == 'DE') {
            $drop['winner'] = $bank_node->nid;
          }
          
          while ($drop['count'] > 0) {
            $drop_node = new stdClass();
            $drop_node->type = 'mewdkp_drop';
            $drop_node->title = $drop['name'] .' - '. $node->title;
            $drop_node->field_mewdkp_raid[0]['nid'] = $node->nid;
            $drop_node->field_mewdkp_mob[0]['nid'] = $mob_node->nid;
            $drop_node->field_mewdkp_item[0]['nid'] = $drop['nid'];
            $drop_node->field_mewdkp_player[0]['nid'] = $drop['winner'];
            $drop_node->field_mewdkp_witnesses = $attendance;
            $drop_node->field_mewdkp_timeline_entry[0]['nid'] = $drop['timeline_nid'];
            $drop_node->field_mewdkp_ded[0]['value'] = ($drop['note'] == 'DE')?(1):(0);
						if (is_numeric($drop['note'])){
							$drop_node->field_mewdkp_price_override[0]['value'] = $drop['note'];
						}
            node_save($drop_node);
            --$drop['count'];
          }
          
        }
        
      }
      
    }
    
  }
  
}

/**
 * Takes a newly created raid, and parses the xml into a timeline
 * @return 
 * @param object $node
 */
function _mewdkp_parse_raid_xml($node) {
  
  _mewdkp_remove_raid($node);
  
  require_once('XMLParser.inc');
  $parser = new XMLParser(utf8_encode($node->field_mewdkp_xml[0]['value']), 'raw', 1);
  
  $tree = $parser->getTree();
  
  #dpm($tree);
  
  $raidinfo = $tree['RAIDINFO'];

  $zone = $raidinfo['ZONE']['VALUE'];
  $zone_node = _mewdkp_zone_load($zone);
  
  static $sex = array();
  if (empty($sex)) {
    $sex = variable_get('mewdkp_lookup_sex',array());
  }
  
  static $race = array();
  if (empty($race)) {
    $race = variable_get('mewdkp_lookup_race',array());
  }
  static $class = array();
  if (empty($class)) {
    $class = variable_get('mewdkp_lookup_class',array());
  }
  
  //Create new players as users
  if ($playerinfo = $raidinfo['PLAYERINFOS']['PLAYER']) {
  
    if (!isset($playerinfo[0])) {
      $players[0] = $playerinfo;
    }
    else {
      $players = $playerinfo;
    }
    
    foreach ($players as $player) {
    
      $name_tmp = $player['NAME']['VALUE'];
      $race_tmp = $player['RACE']['VALUE'];
      $guild_tmp = $player['GUILD']['VALUE'];
      $sex_tmp = $player['SEX']['VALUE'];
      $class_tmp = $player['CLASS']['VALUE'];
      
      $player_user = _mewdkp_player_load($name_tmp,$sex_tmp,$race_tmp,$class_tmp);
      
    }
    
  }
  
  
  $timeline_type = array();    
  $timeline_type[1] = 'Mob Kill';
  $timeline_type[2] = 'Loot Item';
  $timeline_type[3] = 'Player Join';
  $timeline_type[4] = 'Player Leave';

  //Create join events
  if ($joininfo = $raidinfo['JOINS']['JOIN']) {
    
    $tl_type = 3;
    
    if (!isset($joininfo[0])) {
      $joins[0] = $joininfo;
    }
    else {
      $joins = $joininfo;
    }
    
    foreach ($joins as $join) {
    
      $jointime = $join['TIME']['VALUE'];
      $player = $join['PLAYER']['VALUE'];
      
      $joined_player = _mewdkp_player_load($player);
      
      $timeline_node = new stdClass();
      $timeline_node->type = 'mewdkp_timeline';
      $timeline_node->field_mewdkp_raid[0]['nid'] = $node->nid;
      $timeline_node->field_mewdkp_timeline_type[0]['value'] = $tl_type;
      $timeline_node->field_mewdkp_datetime[0]['value'] = $timeline_node->field_mewdkp_datetime[0]['value2'] = date('Y-m-d H:i:s', $jointime);
      
      $timeline_node->field_mewdkp_player[0]['nid'] = $joined_player->nid;
      $timeline_node->field_mewdkp_mob[0]['nid'] = 0;
      $timeline_node->field_mewdkp_item[0]['nid'] = 0;
      
      $timeline_node->title = $timeline_type[$tl_type] .' - '. $joined_player->title;
      node_save($timeline_node);
      
    }
    
  }
  
  //Create leave events
  if ($leaveinfo = $raidinfo['LEAVES']['LEAVE']) {
    
    $tl_type = 4;
    
    if (!isset($leaveinfo[0])) {
      $leaves[0] = $leaveinfo;
    }
    else {
      $leaves = $leaveinfo;
    }
    
    foreach ($leaves as $leave) {
    
      $leavetime = $leave['TIME']['VALUE'];
      $player = $leave['PLAYER']['VALUE'];
      
      $leaving_player = _mewdkp_player_load($player);
      
      $timeline_node = new stdClass();
      $timeline_node->type = 'mewdkp_timeline';
      $timeline_node->field_mewdkp_raid[0]['nid'] = $node->nid;
      $timeline_node->field_mewdkp_timeline_type[0]['value'] = $tl_type;
      $timeline_node->field_mewdkp_datetime[0]['value'] = $timeline_node->field_mewdkp_datetime[0]['value2'] = date('Y-m-d H:i:s', $leavetime);
      
      $timeline_node->field_mewdkp_player[0]['nid'] = $leaving_player->nid;
      $timeline_node->field_mewdkp_mob[0]['nid'] = 0;
      $timeline_node->field_mewdkp_item[0]['nid'] = 0;
      
      $timeline_node->title = $timeline_type[$tl_type] .' - '. $leaving_player->title;
      node_save($timeline_node);
      
    }
    
  }
  
  //Create bosskill events
  if ($bossinfo = $raidinfo['BOSSKILLS']['BOSSKILL']) {
    
    $tl_type = 1;
    
    if (!isset($bossinfo[0])) {
      $bosskills[0] = $bossinfo;
    }
    else {
      $bosskills = $bossinfo;
    }
    
    foreach ($bosskills as $bosskill) {
    
      $killtime = $bosskill['TIME']['VALUE'];
      $boss = $bosskill['NAME']['VALUE'];
      
      $mob_node = _mewdkp_mob_load($boss,$zone_node->title);
      
      $timeline_node = new stdClass();
      $timeline_node->type = 'mewdkp_timeline';
      $timeline_node->field_mewdkp_raid[0]['nid'] = $node->nid;
      $timeline_node->field_mewdkp_timeline_type[0]['value'] = $tl_type;
      $timeline_node->field_mewdkp_datetime[0]['value'] = $timeline_node->field_mewdkp_datetime[0]['value2'] = date('Y-m-d H:i:s', $killtime);
      
      $timeline_node->field_mewdkp_player[0]['nid'] = 0;
      $timeline_node->field_mewdkp_mob[0]['nid'] = $mob_node->nid;
      $timeline_node->field_mewdkp_item[0]['nid'] = 0;
      
      $timeline_node->title = $timeline_type[$tl_type] .' - '. $mob_node->title;
      node_save($timeline_node);
      
    }
    
  }
  
  //Create loot events
  if ($lootinfo = $raidinfo['LOOTS']['LOOT']) {
    
    $tl_type = 2;
    
    if (!isset($lootinfo[0])) {
      $loots[0] = $lootinfo;
    }
    else {
      $loots = $lootinfo;
    }
    
    foreach ($loots as $loot) {
      
      $mob_node = '';
      $item_node = '';
    
      $loottime = $loot['TIME']['VALUE'];
      $itemname = $loot['ITEMNAME']['VALUE'];
      $droppedfrom = $loot['BOSS']['VALUE'];
      $difficulty = $loot['DIFFICULTY']['VALUE'];
      if ($droppedfrom == '') $droppedfrom = "Trash mob"; //eliminates a string error in RaidTracker
      
      $wowid = $loot['ITEMID']['VALUE'];
      $wowid = explode(':', $wowid);
      $wowid = $wowid[0];
      
      $mob_node = _mewdkp_mob_load($droppedfrom,$zone_node->title);
      
      $item_node = node_load(array('type' => 'mewdkp_item', 'title' => $itemname));
      
      if (!($item_node->nid > 0)) {
        $item_node = new stdClass();
        $item_node->type = 'mewdkp_item';
        $item_node->title = $itemname;
        $item_node->field_mewdkp_wowid[0]['value'] = $wowid;
        $item_node->field_mewdkp_mob[0]['nid'] = $mob_node->nid;
        $item_node->field_mewdkp_exclude[0]['value'] = 0;
        $item_node->field_mewdkp_difficulty[0]['value'] = $difficulty;
        node_save($item_node);
        $_REQUEST['destination'] = 'mewdkp/setprices?destination=node/'. $node->nid;
      }
      else {
        /* Dropping from multiple possible sources
         * Scans item, adds new boss mob if found */
        $drop_source_found = false; $i = 0;
        while (!$drop_source_found && $i < count($item_node->field_mewdkp_mob)){
          if ($item_node->field_mewdkp_mob[$i]['nid'] == $mob_node->nid)
            $drop_source_found = true;
          $i++;
        }
        if (!$drop_source_found) {
          $item_node->field_mewdkp_mob[] = array('nid' => $mob_node->nid);
        }
        node_save($item_node);
      }
      // -- End multiple source mob code
      
      /* Removed the following code.  I believe mutliple drop sources
         renders the need for this failsafe irrelevant.
         
      elseif ($item_node->field_mewdkp_mob[0]['nid'] != $mob_node->nid) {
        $trash_mob_node = _mewdkp_mob_load('Trash mob');
        $item_node->field_mewdkp_mob[0]['nid'] = $trash_mob_node->nid;
        node_save($item_node);
      }
      */
      
      
      $winning_player = _mewdkp_player_load($loot['PLAYER']['VALUE']);
      
      $timeline_node = new stdClass();
      $timeline_node->type = 'mewdkp_timeline';
      $timeline_node->field_mewdkp_raid[0]['nid'] = $node->nid;
      $timeline_node->field_mewdkp_timeline_type[0]['value'] = $tl_type;
      $timeline_node->field_mewdkp_datetime[0]['value'] = $timeline_node->field_mewdkp_datetime[0]['value2'] = date('Y-m-d H:i:s', $loottime);
      
      $timeline_node->field_mewdkp_player[0]['nid'] = $winning_player->nid;
      $timeline_node->field_mewdkp_mob[0]['nid'] = $mob_node->nid;
      $timeline_node->field_mewdkp_item[0]['nid'] = $item_node->nid;
      $timeline_node->field_mewdkp_count[0]['value'] = ($loot['COUNT']['VALUE'] > 0)?($loot['COUNT']['VALUE']):(1);
      $timeline_node->field_mewdkp_note[0]['value'] = $loot['NOTE']['VALUE'];
      
      $timeline_node->title = $timeline_type[$tl_type] .' - '. $item_node->title;
      node_save($timeline_node);
      
    }
    
  }
  
}
